//
//  SideViewController.m
//  fitnessmanage
//
//  Created by wujian on 2017/10/29.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "SideViewController.h"
#import "SideTableViewCell.h"
#import "AddSideViewController.h"
#import "AddSideDataViewController.h"

@interface SideViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    int startIndex;
    
    int pageSize;
}

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)NoDataView *noDataView;

@end

@implementation SideViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title  = @"体侧表模板";
    
    [self createSubView];
    
    //[self loadStartData];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon_navadd"]
                                                                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(add)];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    [NotificationCenter addObserver:self selector:@selector(loadStartData) name:@"ReloadSide" object:nil];
}

-(void)createSubView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.estimatedRowHeight = 200;
    [tableView registerClass:[SideTableViewCell class] forCellReuseIdentifier:@"SideTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = BackGroundColor;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
    }];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.automaticallyHidden = YES;
    
    
    MyTableHead *mj_head = [MyTableHead headerWithRefreshingTarget:self refreshingAction:@selector(loadStartData)];
    tableView.mj_header = mj_head;
    tableView.mj_footer = footer;
    [tableView.mj_header beginRefreshing];

}

#pragma mark 下拉
-(void)loadStartData{
    
    startIndex = 1;
    
    pageSize = 20;
    
    [self loadData];
}

#pragma mark 上拉
-(void)loadMoreData{
    
    startIndex = startIndex + 1;
    
    [self loadData];
}

#pragma mark 加载数据
-(void)loadData{
    
    //加载数据
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];


    [params setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"ShopID"] forKey:@"ShopID"];
    
    [params setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"UserID"] forKey:@"MemberID"];

    

    [params setObject:[NSString stringWithFormat:@"%d",startIndex] forKey:@"page"];

    [params setObject:[NSString stringWithFormat:@"%d",pageSize] forKey:@"rows"];

    [HTTPTool GET:Get_BodyData_ListData parameters:params

          success:^(id responseObject){

              [MBProgressHUD hideHUDForView:self.view];
              
              
              NSLog(@"%@",responseObject);

              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {

                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];

                  if (resultArray.count > 0) {

                      [self.tableView.mj_header endRefreshing];

                      [self.tableView.mj_footer endRefreshing];
                  }
                  else{

                      [self.tableView.mj_header endRefreshing];

                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                  }

                  self.noDataView = [NoDataView shareInstance];

                  [self.noDataView hideInView:self.view];

                  if (startIndex == 1) {

                      self.dataSource = [NSMutableArray arrayWithArray:resultArray];

                      if (self.dataSource.count == 0) {

                          self.noDataView = [NoDataView shareInstance];

                          [self.noDataView showInView:self.view];
                      }
                  }
                  else{

                      [self.dataSource addObjectsFromArray:resultArray];
                  }

                  [self.tableView reloadData];
              }

          }

          failure:^(NSError *error){

              [self.tableView.mj_header endRefreshing];

              [MBProgressHUD hideHUDForView:self.view];
          }];

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSource.count ;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}


#pragma mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 30;
    }
    
    return 15;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SideTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SideTableViewCell"];
    
    NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.section];
    
    cell.titleLabel.text = [dict objectForKey:@"Name"];
//
    cell.bottomLabel.text = [NSString stringWithFormat:@"创建时间：%@",[dict objectForKey:@"AddTime"]];
    
    cell.detailLabel.text = [NSString stringWithFormat:@"创  建  人：%@",[dict objectForKey:@"UserName"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.from isEqualToString:@"vipInfo"]) {

        NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.section];

        AddSideDataViewController *addSideDataVC = [[AddSideDataViewController alloc] init];

        addSideDataVC.bodyDetailDict = dict;

        addSideDataVC.venueVipModel = self.venueVipModel;

        [self.navigationController pushViewController:addSideDataVC animated:YES];
    }
    else{

        NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.section];

        AddSideViewController *addSideVC = [[AddSideViewController alloc] init];

        addSideVC.sideDict = dict;

        [self.navigationController pushViewController:addSideVC animated:YES];
    }
}


#pragma mark 添加模板
-(void)add{
    
    if ([self.from isEqualToString:@"vipInfo"]) {
        
        AddSideDataViewController *addSideDataVC = [[AddSideDataViewController alloc] init];
        
        addSideDataVC.venueVipModel = self.venueVipModel;
        
        [self.navigationController pushViewController:addSideDataVC animated:YES];
    }
    else{
        
        AddSideViewController *addSideVC =[[AddSideViewController  alloc] init];
        
        [self.navigationController pushViewController:addSideVC animated:YES];
    }
}

#pragma mark Get
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
    }
    
    return _dataSource;
}

-(void)dealloc{
    
    [NotificationCenter removeObserver:self];
}

@end
